package com.defysoft.waukidsschool.main;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukidsschool.BaseActivity;
import com.defysoft.waukidsschool.R;
import com.defysoft.waukidsschool.Wake_Screen;
import com.defysoft.waukidsschool.scanmode.Scanmode_SetTime;

public class Main_Intro extends BaseActivity {

	private SharedPreferences login_code;
	private String sch_code = null;
	private String sch_name = null;
	final int SUCCESS = 10;
	final int FAIR = 9;
	final int LOGIN_STARTACTIVITY = 8;
	int resultint = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
						| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
						| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
						| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

		getWindow().setFormat(PixelFormat.TRANSPARENT);
		setContentView(R.layout.activity_main);
		Wake_Screen.acquireCpuWakeLock(this);
		setStartData();
		try {
			resultint = getIntent().getIntExtra("AUTOSTART", 0);
		} catch (Exception e) {

		}
	}

	@Override
	public void setStartData() {
		super.setStartData();
		((TextView) findViewById(R.id.WAUKIDSCHOOL_VERSION))
				.setText("appversion : " + getVersion()); // 버전표시.
		login_code = getSharedPreferences("login_code", MODE_PRIVATE);
		try {
			sch_code = login_code.getString("sch_code", null);
			sch_name = login_code.getString("sch_name", null);
		} catch (Exception e) {
			sch_code = null;
			sch_name = null;
		}
		if (sch_code == null || sch_code == "" || sch_name == null
				|| sch_name == "") {
			startThread(2);
		} else {
			startThread(1);
		}
	}

	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread = new Thread(new Runnable() {
			@Override
			public void run() {
				if (checkVersion()) {
					switch (cho) {
					case 1:
						mainThread();
						break;
					case 2:
						try {
							Thread.sleep(2000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						onNetworkCallback(LOGIN_STARTACTIVITY, "");
						break;

					}
				}
			}
		});
		firstThread.start();
	}

	private void mainThread() {
		// TODO Auto-generated method stub
		String msg = "";
		boolean loof=true;
		while (loof) {

			try {
				Thread.sleep(2000);
				JSONObject jsonObject = new JSONObject(publicHttpCallback(
						HTTP_LOGIN_CHECK, "code=" + sch_code + "&name="
								+ sch_name));
				if (jsonObject.getBoolean("success")) {
					msg = jsonObject.getString("payload");
					onNetworkCallback(SUCCESS, msg);
				} else {
					onNetworkCallback(FAIR, jsonObject.getString("message"));
				}
				loof=false;
			} catch (Exception e) {
				onNetworkCallback(CATCH_EXCEPTION, e.toString());
			}
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void handlingMessage(Message _message) {
		// TODO Auto-generated method stub
		super.handlingMessage(_message);
		switch (_message.what) {
		case CATCH_EXCEPTION:
			endDIal();
			
			Toast.makeText(this, "네트워크오류 5초후 다시통신시도",
					Toast.LENGTH_SHORT).show();
			break;
		case FAIR:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			sendMessage(LOGIN_STARTACTIVITY);
			finish();
			break;
		case SUCCESS:
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			startActivity(new Intent(this, Scanmode_SetTime.class).putExtra("AUTOSTART",
					resultint));
			finish();
			break;
		case LOGIN_STARTACTIVITY: // 저장된 정보 없거나 잘못되서 로그인페이지로 이동
			endDIal();
			if (firstThread != null) {
				firstThread.interrupt();
			}
			startActivity(new Intent(this, Main_login.class)
					.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			finish();
			break;

		}
	}

	private String getVersion() { // 매니페스트 버전이름 들고옴.
		PackageInfo pi = null;
		try {
			pi = getPackageManager().getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return pi.versionName;
	}

	private boolean checkVersion() {
		return true;

	}
}
